package com.fr.data.impl.sap;

import com.fr.base.ParameterHelper;
import com.fr.general.data.DataModel;
import com.fr.script.Calculator;
import com.fr.stable.ParameterProvider;
import com.fr.stable.StringUtils;
import com.fr.stable.xml.XMLPrintWriter;
import com.fr.stable.xml.XMLableReader;

public class SimpleGeneralQuerySAPTableData extends SAPTableData {
    private String tableName;
    private String selectClaus;
    private String whereClaus;

    public void setTableName(String tableName) {
        this.tableName = tableName;
    }

    public String getTableName() {
        return this.tableName;
    }

    public void setSelectClaus(String selectClaus) {
        this.selectClaus = selectClaus;
    }

    public String getSelectClaus() {
        return this.selectClaus;
    }

    public void setWhereClaus(String whereClaus) {
        this.whereClaus = whereClaus;
    }

    public String getWhereClaus() {
        return this.whereClaus;
    }

    public DataModel createDataModel(Calculator calculator) {
        if (StringUtils.isBlank(this.tableName)
                || StringUtils.isBlank(this.selectClaus)) {
            return null;
        }

        ParameterProvider[] ps = this.processParameters(calculator);

        String tname = this.trimString(ParameterHelper.analyzeCurrentContextTableData4Templatee(this.tableName, ps));
        String[] ss = this.rightString(this.selectClaus, ps);

        String[] ws = this.rightString(this.whereClaus, ps);

        this.rightWhereString(ws);

        return new SimpleGeneralQuerySAPDataModel(connectionName, tname, ss, ws);
    }

    public void readXML(XMLableReader reader) {
        super.readXML(reader);
        if (reader.isChildNode()) {
            String tmpVal;
            String nodeName = reader.getTagName();
            if ("SGQSAPTableDataAttr".equals(nodeName)) {
                if ((tmpVal = reader.getAttrAsString("tableName", null)) != null) {
                    this.setTableName(tmpVal);
                }
            } else if ("WCS".equals(nodeName)) {
                if ((tmpVal = reader.getElementValue()) != null) {
                    this.setWhereClaus(tmpVal);
                }
            } else if ("SCS".equals(nodeName)) {
                if ((tmpVal = reader.getElementValue()) != null) {
                    this.setSelectClaus(tmpVal);
                }
            }
        }
    }

    public void writeXML(XMLPrintWriter writer) {
        super.writeXML(writer);
        writer.startTAG("SGQSAPTableDataAttr");
        if (this.tableName != null) {
            writer.attr("tableName", this.getTableName());
        }
        writer.end();
        if (this.selectClaus != null) {
            writer.startTAG("SCS");
            writer.textNode(this.selectClaus);
            writer.end();
        }
        if (this.whereClaus != null) {
            writer.startTAG("WCS");
            writer.textNode(this.whereClaus);
            writer.end();
        }
    }
}
